'''
版权: Copyright (c) 2018 red

文件: gov_app.py
创建日期: Friday December 14th 2018
作者: pao
说明:
1、政务APP
'''
from pao.app import BaseApp, JsonRpcApp
from gov.data_process.dlh_screen_display.scheduler_data import InsertData
from gov.data_process.tire_pressure.scheduler_TP_data import InsertData_TP
from gov.data_process.dlh_screen_display.sim_basic_data import Gener_Sim_Data
from apscheduler.schedulers.blocking import BlockingScheduler
import gov.register.dlh_screen_display_func as getdata_dlh
import gov.register.tire_pressure_func as getdata_tp
#import gov.register.gov_netthing_func as getdata_gov
import threading
import datetime

# 政务APP类


class GovApp(JsonRpcApp):
    def __init__(self, app_name, address, port, db_addr, db_port):
        JsonRpcApp.__init__(self, app_name, address, port)
        self.db_addr = db_addr
        self.db_port = db_port

    def on_regsiter_services(self, jsonrpc):
        # 注册getdata服务
        getdata_dlh.register(jsonrpc, self.db_addr, self.db_port)
        getdata_tp.register(jsonrpc, self.db_addr, self.db_port)
        #getdata_gov.register(jsonrpc, self.db_addr, self.db_port)


class GOVTPDataSimulateApp(BaseApp):
    def __init__(self, app_name, db_addr, db_port, destination_loc):
        BaseApp.__init__(self, app_name)
        self.db_addr = db_addr
        self.db_port = db_port
        self.destination_loc = destination_loc

    def run(self):
        # 执行胎压数据的插入线程
        insertdata = InsertData_TP(
            db_addr=self.db_addr, db_port=self.db_port, destination_loc=self.destination_loc)
        threads = []
        t1 = threading.Thread(target=insertdata.gener_TP_monitoring)
        threads.append(t1)
        t1.start()


class GovDataSimulateApp(BaseApp):
    def __init__(self, app_name, db_addr, db_port, dlh_equip_num, cal_A):
        BaseApp.__init__(self, app_name)
        self.db_addr = db_addr
        self.db_port = db_port
        self.dlh_equip_num = dlh_equip_num
        self.cal_A = cal_A

    def run(self):
        # 同步执行3个线程
        insertdata = InsertData(db_addr=self.db_addr, db_port=self.db_port,
                                dlh_equip_num=self.dlh_equip_num, cal_A=self.cal_A)
        threads = []
        t1 = threading.Thread(target=self.thread_main_ydb)
        threads.append(t1)
        t2 = threading.Thread(target=self.thread_main_equip)
        threads.append(t2)
        t3 = threading.Thread(target=insertdata.gener_ycdata)
        threads.append(t3)
        t1.start()
        t2.start()
        t3.start()

    def thread_TJydb(self):
        # 线程1的子线程，先执行TJ_ydb的补充数据程序，再执行TJ_ydb的实时插入程序
        main1 = InsertData(db_addr=self.db_addr, db_port=self.db_port,
                           dlh_equip_num=self.dlh_equip_num, cal_A=self.cal_A)
        main1.buchong_TJ_ydb()
        main1.gener_data_TJydb()

    def thread_main_ydb(self):
        # 线程1，ydb的数据补充完成后，同步进行ydb的实时数据插入，TJ_ydb数据的补充和插入，2分钟生成一次的实时数据
        main1 = InsertData(db_addr=self.db_addr, db_port=self.db_port,
                           dlh_equip_num=self.dlh_equip_num, cal_A=self.cal_A)
        main1.buchong_ydb()
        threads = []
        t1 = threading.Thread(target=main1.gener_data_ydb)
        threads.append(t1)
        t2 = threading.Thread(target=self.thread_TJydb)
        threads.append(t2)
        t3 = threading.Thread(target=main1.gener_2mins)
        threads.append(t3)
        t1.start()
        t2.start()
        t3.start()

    def thread_main_equip(self):
        # 线程2，analy_equip数据补充完成后开始执行实时生成
        main1 = InsertData(db_addr=self.db_addr, db_port=self.db_port,
                           dlh_equip_num=self.dlh_equip_num, cal_A=self.cal_A)
        main1.buchong_equip()
        main1.gener_data_equip()
